package studentsinfomanagement.dao;

import studentsinfomanagement.bean.Course;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 课程数据类
 *
 */
public class CourseDAO extends BaseDAO {
    private static CourseDAO cd = null;

    /**
     * 单例模式
     */
    private CourseDAO(){}

    /**
     * 获取CourseDAO的实例
     * @return 实例
     */
    public static CourseDAO getInstance(){
        if(cd == null){
            cd = new CourseDAO();
        }
        return cd;
    }

    /**
     * 根据课程号获取课程信息
     * @param cno 课程号
     * @return 课程信息
     */
    public Course getCourseByCno(String cno){
        //准备sql查询语句
        String sql = "select * from course where cno=?";
        //准备参数
        String[] param = { cno };
        //调用数据库工具类的查询方法，执行查询语句
        rs = db.executeQuery(sql, param);
        Course result = null;
        try{
            if (rs.next()) {//查询到课程
                result = new Course();
                result.setCno(cno);
                result.setName(rs.getString("name"));
                result.setTno(rs.getString("tno"));
                result.setScore(rs.getInt("score"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            destroy();//关闭resultSet
        }
        return result;
    }

    /**
     * 获取全部课程信息
     * @return 全部课程信息
     */
    public List<Course> getCourses(){
        List<Course> cList = new ArrayList<>();
        //准备sql查询语句
        String sql = "select * from course";
        //调用数据库工具类的查询方法，执行查询语句
        rs = db.executeQuery(sql);
        try{
            while (rs.next()) {//查询到课程
                Course course = new Course();
                course.setCno(rs.getString("cno"));
                course.setName(rs.getString("name"));
                course.setTno(rs.getString("tno"));
                course.setScore(rs.getInt("score"));
                cList.add(course);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            destroy();//关闭resultSet
        }
        return cList;
    }

    /**
     * 获取指定教师教授的全部课程列表
     * @param tno 教师教工号
     * @return 课程列表
     */
    public List<Course> getCoursesByTno(String tno){
        List<Course> courseList = new ArrayList<>();
        //准备sql查询语句
        String sql = "select * from course where tno=?";
        //准备参数
        String[] param = { tno };
        //调用数据库工具类的查询方法，执行查询语句
        rs = db.executeQuery(sql,param);
        try{
            while (rs.next()) {//查询到课程
                Course course = new Course();
                course.setCno(rs.getString("cno"));
                course.setName(rs.getString("name"));
                course.setTno(rs.getString("tno"));
                course.setScore(rs.getInt("score"));
                courseList.add(course);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            destroy();//关闭resultSet
        }
        return courseList;
    }

    /**
     * 通过课程名和授课老师教工号获取课程信息
     * @param name 课程名字
     * @param tno 教工号
     * @return 老师对象
     */
    public Course getCourseByName(String name, String tno){
        //准备sql查询语句
        String checkSql = "select * from course where name=? and tno=?";
        //准备参数
        String[] checkParam = { name, tno};
        //调用数据库工具类的查询方法，执行查询语句
        rs = db.executeQuery(checkSql, checkParam);
        Course result = null;
        try {
            if(rs.next()){
                result = new Course();
                result.setCno(rs.getString("cno"));
                result.setName(name);
                result.setTno(tno);
                result.setScore(rs.getInt("score"));
            }
        } catch (SQLException e) {
            result = null;
        }
        return result;
    }

    /**
     * 删除课程信息
     * @param course 课程对象
     * @return 操作结果
     */
    public boolean delete(Course course) {
        boolean result = false;
        if (course == null) {
            return result;
        }
        String sql = "delete from course where cno=?";
        String[] param = { course.getCno() };
        int rowCount = db.executeUpdate(sql, param);
        if (rowCount == 1) {
            result = true;
        }
        destroy();
        return result;
    }

}
